//
//  ClassSettingView.m
//  GIFShow
//
//  Created by lanou11air029 on 15/4/30.
//  Copyright (c) 2015年 CK. All rights reserved.
//

#import "ClassSettingView.h"
#import "MoveView.h"
#import "MySettingOptions.h"

typedef NS_ENUM(NSUInteger, TitlePanChangeType) {
    TitleChangeFromLikeView,
    TitleChangeFromOtherView,
    TitleChangeSortInLikeView,
    TitleChangeSortInOtherView,
};
@interface ClassSettingView ()<UIGestureRecognizerDelegate>
@property (nonatomic) TitlePanChangeType ClassViewChangeType;
@property (nonatomic, strong) NSMutableArray * rectArray;
@property (nonatomic, strong) NSMutableArray * sortArray;
@property (nonatomic, strong) UIPanGestureRecognizer * panGesture;
@property (nonatomic, strong) UILabel * moreClassLabel;
@property (nonatomic, strong) MoveView * staticView;
@property (nonatomic, assign) CGRect beginFrame;
@end
@implementation ClassSettingView
- (instancetype)init
{
    CGRect frame = [UIScreen mainScreen].bounds;
    frame.origin.y = 400;
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
    }
    return self;
}
-(NSMutableArray *)rectArray{
    if (_rectArray != nil) {
        return _rectArray;
    }
    _rectArray = [[NSMutableArray alloc]init];
    return _rectArray;
}


-(void)setupSubviews{
    self.backgroundColor = [UIColor clearColor];
    self.tag = YES;
    self.moreClassLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 30)];
    self.moreClassLabel.text = @"更多类别";
    self.doneButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.doneButton setTitle:@"完成" forState:UIControlStateNormal];
    [self.doneButton setBackgroundColor:[UIColor colorWithRed:15.0/255 green:120.0/255 blue:0.9 alpha:1]];
    [self.doneButton setTintColor:[UIColor colorWithWhite:1 alpha:0.95]];
    self.doneButton.frame = CGRectMake(5, self.frame.size.height - 50, self.frame.size.width - 10, 40);
    self.doneButton.layer.masksToBounds = YES;
    self.doneButton.layer.cornerRadius = 5;
//    [self addSubview:self.UpView];
//    [self addSubview:self.DownView];
}
-(NSMutableArray *)sortArray{
    if (_sortArray) {
        return _sortArray;
    }
    _sortArray = [[NSMutableArray alloc]init];
    return _sortArray;
}

-(void)setUserLikeArray:(NSMutableArray *)userLikeArray{
    if (_userLikeArray != userLikeArray) {
        _userLikeArray = userLikeArray;
        CGFloat margin = self.frame.size.width * 0.04;
        
        for (int i = 0; i < _userLikeArray.count; i++) {
            int row = i/3;
            int col = i%3;
            MoveView * classView = [[MoveView alloc]initWithFrame:CGRectMake(margin+self.frame.size.width*(0.32)*col, 10+row*30, self.frame.size.width*0.3-margin, 25)];
            classView.titleName = self.userLikeArray[i];
            classView.userInteractionEnabled = YES;
            [self addSubview:classView];
            [self.rectArray addObject:[NSValue valueWithCGRect:classView.frame]];
            [self.sortArray  addObject:[NSNumber numberWithInteger:[self.subviews indexOfObject:classView]]];
            classView.tag = i + 1000;
            if (i==0) {
                self.staticView = classView;
            
            }
            if (i>0) {
//                UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(moveTheViewAction:)];
//                [classView addGestureRecognizer:pan];
                UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapTheViewAction:)];
                [classView addGestureRecognizer:tap];
            }
            
        }

    }
}
-(void)setOtherArray:(NSMutableArray *)otherArray{
    if (_otherArray != otherArray) {
        _otherArray = otherArray;
        CGFloat margin = self.frame.size.width * 0.04;
        
        for (int i = 0; i < _otherArray.count; i++) {
            int row = i/3;
            int col = i%3;
            CGFloat userLikeHeight = self.userLikeArray.count / 3 * 30 + 60;
            MoveView * classView = [[MoveView alloc]initWithFrame:CGRectMake(margin+self.frame.size.width*(0.32)*col, 10+row*30+userLikeHeight, self.frame.size.width*0.3-margin, 25)];
            classView.titleName = self.otherArray[i];
            classView.userInteractionEnabled = YES;
            [self addSubview:classView];
            [self.rectArray addObject:[NSValue valueWithCGRect:classView.frame]];
            [self.sortArray  addObject:[NSNumber numberWithInteger:[self.subviews indexOfObject:classView]]];

            classView.tag = i + 2000;
//            UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(moveTheViewAction:)];
//            [classView addGestureRecognizer:pan];
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapTheViewAction:)];
            [classView addGestureRecognizer:tap];
        }

    }
}
//-(void)moveTheViewAction:(UIPanGestureRecognizer *)panGes{
//    self.panGesture = panGes;
//    if(panGes.view == self.staticView){
//        return;
//    }
//    CGPoint local = [panGes locationInView:self];
//    panGes.view.center = local;
//    if (panGes.state == UIGestureRecognizerStateBegan) {
//        self.beginFrame = panGes.view.frame;
//
//    }
//
//        for (UIView * view in self.subviews) {
//            if (view == panGes.view) {
//                continue;
//            }
//        if (CGRectIntersectsRect(panGes.view.frame, view.frame)) {
//           CGRect interRect = CGRectIntersection(panGes.view.frame, view.frame);
//            if (interRect.size.width > panGes.view.frame.size.width * 0.5 && interRect.size.height > panGes.view.frame.size.height * 0.5) {
//                [self subViewTitlesReLayoutTouchView:(MoveView*)panGes.view intersectView:(MoveView*)view];
//            }
//        }
//        }
//    if (panGes.state == UIGestureRecognizerStateEnded) {
//
//                   self.tag = NO;
//            [UIView animateWithDuration:0.2 animations:^{
//                for (int i = 0; i < self.subviews.count; i++) {
//                    UIView * view = self.subviews[i];
//                    view.frame = [self.rectArray[i] CGRectValue];
//                }
//            } completion:^(BOOL finished){
//                self.tag = YES;
//            }];
////        }
//    }
//}


-(void)subViewTitlesReLayoutTouchView:(MoveView * )touchView intersectView:(MoveView * )intersectView{
    if (self.tag == NO) {
        return;
    }
    self.tag = NO;
    if (touchView == self.staticView || intersectView == self.staticView) {
        return;
    }
    NSInteger i = [self.subviews indexOfObject:touchView];
    NSInteger j = [self.subviews indexOfObject:intersectView];
    

    if ([self.userLikeArray containsObject:touchView.titleName]) {
        int iTouch = [self.userLikeArray indexOfObject:touchView.titleName];
        if ([self.userLikeArray containsObject:intersectView.titleName]) {
            int iInterset = [self.userLikeArray indexOfObject:intersectView.titleName];
            
            [self.userLikeArray exchangeObjectAtIndex:iTouch withObjectAtIndex:iInterset];
        }else{
            int iInterset = [self.otherArray indexOfObject:intersectView.titleName];
            
            [self.userLikeArray removeObject:touchView.titleName];
            [self.userLikeArray insertObject:intersectView.titleName atIndex:iTouch];
            [self.otherArray removeObject:intersectView.titleName];
            [self.otherArray insertObject:touchView.titleName atIndex:iInterset];
        }
    }
    
    [self.rectArray exchangeObjectAtIndex:i withObjectAtIndex:j];
    [self.sortArray exchangeObjectAtIndex:i withObjectAtIndex:j];
    [UIView animateWithDuration:0.3 animations:^{
        MoveView * mView = (MoveView *)self.subviews[j];
        mView.frame = [self.rectArray[j] CGRectValue];
    }];
    
    self.tag = YES;
}

-(void)tapTheViewAction:(UITapGestureRecognizer*)tapGesture{
    CGFloat margin = self.frame.size.width * 0.04;
    [self.rectArray removeAllObjects];
    MoveView * mView = (MoveView *)tapGesture.view;
    if ([self.userLikeArray containsObject:mView.titleName]) {
        [self.otherArray addObject:mView.titleName];
        [self.userLikeArray removeObject:mView.titleName];
    }else{
        [self.userLikeArray addObject:mView.titleName];
        [self.otherArray removeObject:mView.titleName];
    }
    
    
        int i = 0;
        for (NSString * name in self.userLikeArray) {
            for (MoveView * move in self.subviews) {
                if ([move.titleName isEqualToString:name]) {
                    int col = i % 3;
                    int row = i / 3;
                    i++;
                    [UIView animateWithDuration:0.3 animations:^{
                        move.frame = CGRectMake(margin+self.frame.size.width*(0.32)*col, 10+row*30, self.frame.size.width*0.3-margin, 25);
                    }];
                    [self.rectArray addObject:[NSValue valueWithCGRect:move.frame]];
                }
            }
        }
    int num = i / 3;
    num = num * 3;
    self.moreClassLabel.center = CGPointMake(margin+100, num/3*30+30+88);

        for (NSString * name in self.otherArray) {

            for (MoveView * move in self.subviews) {
                if ([move.titleName isEqualToString:name]) {
                    int col = num % 3;
                    int row = num / 3;
                    num++;
                    NSLog(@"%d",num);
                    CGFloat userLikeHeight = 60;
                    [UIView  animateWithDuration:0.3 animations:^{
                        move.frame = CGRectMake(margin+self.frame.size.width*(0.32)*col, 10+row*30+userLikeHeight, self.frame.size.width*0.3-margin, 25);
                    }];
                    [self.rectArray addObject:[NSValue valueWithCGRect:move.frame]];


                }
            }
        }
}


    

-(void)animationToShow{
    CGRect frame = [UIScreen mainScreen].bounds;
    frame.origin.y = 64;
    [UIView animateWithDuration:0.35 animations:^{
        self.frame = frame;
        self.backgroundColor = [UIColor colorWithWhite:1 alpha:0.87];
        self.moreClassLabel.center = CGPointMake(100 + self.frame.size.width * 0.04, self.userLikeArray.count/3*30+88+30);
        [self.window addSubview:self.moreClassLabel];
        [self.window addSubview:self.doneButton];
//        self.UpView.frame = CGRectMake(0, 0, self.bounds.size.width, 30*self.userLikeArray.count/3+30);
//        self.DownView.frame = CGRectMake(0, self.UpView.frame.size.height+self.UpView.frame.origin.y, self.bounds.size.width, 30*self.otherArray.count/3+30);
    }];
}

-(void)removeFromSuperview{
    CGRect frame = [UIScreen mainScreen].bounds;
    
    frame.origin.y = 364;
    
    NSArray * titleArray = [NSArray arrayWithArray:self.userLikeArray];
    [[MySettingOptions defaultOptions].userDefault setObject:titleArray forKey:@"titles"];
[UIView animateWithDuration:0.3 animations:^{
    self.frame = frame;
    self.backgroundColor = [UIColor clearColor];
} completion:^(BOOL finished) {
    [self.moreClassLabel removeFromSuperview];
    [self.doneButton removeFromSuperview];
    [super removeFromSuperview];
}];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
